package com.lysj.bhp.borrowing.service;

import com.lysj.bhp.borrowing.base.BorrowingContent;
import com.lysj.bhp.borrowing.domain.BorrowingRecord;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/9/4--13:57
 * @Description:
 */
@Service
@Transactional
public class BorrowingRecordService extends BorrowingContent {
    /**
     * @author zwj
     * @date 2018-09-04 16:50
     * @@Description:审批，放款操作
     */
    public Resp borrowingRecordApproval(BorrowingRecord borrowingRecord,Integer checkStatus){
        borrowingRecord.setCheckStatus(checkStatus);
        if(checkStatus.equals(-1)){
            walletAvailBanceAndPledgeFreezeBalanceOperation(borrowingRecord.getMortgageType(), borrowingRecord.getUserId(), borrowingRecord.getMortgageMoney() ,borrowingRecord.getMortgageMoney().multiply(new BigDecimal(-1)));
        }
        borrowingRecordRepository.save(borrowingRecord);
        return new Resp("操作成功!");
    }
    /**
     * @author zwj
     * @date 2018-09-04 16:51
     * @@Description：撤销操作
     */
    public Resp borrowingRecordRevocation(Long borrowingRecordId){
        BorrowingRecord borrowingRecord = borrowingRecordRepository.findOne(borrowingRecordId);
        if(borrowingRecord == null){
            return new Resp(Resp.PARAM_ERROR,"操作失败,该借贷记录不存在!");
        }
        if(borrowingRecord.getBorrowingRecordId().equals(currentUser().getUserId())){
            return new Resp(Resp.PARAM_ERROR,"操作失败!");
        }
        walletAvailBanceAndPledgeFreezeBalanceOperation(borrowingRecord.getMortgageType(), borrowingRecord.getUserId(), borrowingRecord.getMortgageMoney() ,borrowingRecord.getMortgageMoney().multiply(new BigDecimal(-1)));
        borrowingRecordRepository.delete(borrowingRecord);
        return new Resp("撤销成功!");
    }
    /**
     * @author zwj
     * @date 2018-09-04 14:34
     * @@Description:钱包可用资产，冻结资产的改变
     */
    public Boolean walletAvailBanceAndPledgeFreezeBalanceOperation(Integer walletType, Long userId, BigDecimal availBalance ,BigDecimal pledgeFreezeBalance){
        switch (walletType) {
            case 10:
                BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(userId);
                bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().add(availBalance));
                bitCoinWallet.setPledgeFreezeBalance(bitCoinWallet.getPledgeFreezeBalance().add(pledgeFreezeBalance));
                bitcoinWalletRepository.save(bitCoinWallet);
                break;
            case 20:
                EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(userId);
                ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().add(availBalance));
                ethereumWallet.setPledgeFreezeBalance(ethereumWallet.getPledgeFreezeBalance().add(pledgeFreezeBalance));
                ethereumWalletRepository.save(ethereumWallet);
                break;
            case 100:
                BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(userId);
                bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().add(availBalance));
                bhpcWallet.setPledgeFreezeBalance(bhpcWallet.getPledgeFreezeBalance().add(pledgeFreezeBalance));
                bhpcWalletRepository.save(bhpcWallet);
                break;
        }
        return true;
    }
    /**
     * @author zwj  
     * @date 2018-09-04 15:30  
     * @@Description:
     */  

}
